package doh.client;

import javax.swing.table.*;
import java.util.*;
import doh.service.*;
import doh.util.*;
import doh.entity.*;

public class HistoryPanel extends javax.swing.JPanel {
    private TableModel model_history;
    private UserSessionBeanRemote sessionBean;

    /** Creates new form HistoryPanel */
    public HistoryPanel(UserSessionBeanRemote aBean, String player) {
        sessionBean = aBean;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        HistoryTableScrollPane = new javax.swing.JScrollPane();
        HistoryTable = new javax.swing.JTable();
        jScrollBar1 = new javax.swing.JScrollBar();
        HistoryLabel = new javax.swing.JLabel();
        HistoryButton = new javax.swing.JButton();
        HistoryField = new javax.swing.JTextField();

        String[] historyHeader = {"Player 1", "P1 Rating", "Player 2", "P2 Rating", "ELO Adj.", "Winner"};
        Object [][] historyContent = new Object[0][6];
        model_history = new DefaultTableModel(historyContent, historyHeader);
        HistoryTable.setModel(model_history);
        HistoryTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        HistoryTable.setCellSelectionEnabled(false);
        HistoryTable.setRowSelectionAllowed(true);
        HistoryTableScrollPane.setViewportView(HistoryTable);

        HistoryLabel.setText("Player ID:");

        HistoryButton.setText("Fetch History");
        HistoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HistoryButtonActionPerformed(evt);
            }
        });

        HistoryField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HistoryFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(HistoryTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 737, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(HistoryLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(HistoryField, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(HistoryButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HistoryLabel)
                    .addComponent(HistoryField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(HistoryButton))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(HistoryTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(143, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void HistoryFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HistoryFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_HistoryFieldActionPerformed

    private void HistoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HistoryButtonActionPerformed
        String[] historyHeader = {"Player 1", "P1 Rating", "Player 2", "P2 Rating", "ELO Adj.", "Winner"};
        List<Game> history = sessionBean.getHistory(HistoryField.getText());
        int tableLength = history.size();
        Object [][] historyContent = new Object[tableLength][6];
        Iterator itr = history.iterator();
        int ii = 0;
        while (itr.hasNext()) {
            Game aGame = (Game)itr.next();
            historyContent[ii][0] = aGame.get_p1();
            historyContent[ii][1] = aGame.get_r1();
            historyContent[ii][2] = aGame.get_p2();
            historyContent[ii][3] = aGame.get_r2();
            historyContent[ii][4] = aGame.get_adjustment();
            historyContent[ii][5] = aGame.get_winner();
            ii++;
        }
        model_history = new javax.swing.table.DefaultTableModel(historyContent, historyHeader);
    }//GEN-LAST:event_HistoryButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton HistoryButton;
    private javax.swing.JTextField HistoryField;
    private javax.swing.JLabel HistoryLabel;
    private javax.swing.JTable HistoryTable;
    private javax.swing.JScrollPane HistoryTableScrollPane;
    private javax.swing.JScrollBar jScrollBar1;
    // End of variables declaration//GEN-END:variables

}
